package com.design.interpreter;

/**
 * @author leali
 * @title: LoopCommandNode
 * @projectName design
 * @description: 循环命令节点类：非终结符表达式
 * @date 2022/1/3 19:01
 */
public class LoopCommandNode extends Node {
    private int number;
    private Node commandNode;

    @Override
    public void interpret(Context context) {
        context.skipToken("LOOP");
        //循环次数
        number = context.currentNumber();
        context.nextToken();
        //循环语句中的表达式
        commandNode = new ExpressionNode();
        commandNode.interpret(context);
    }

    @Override
    public void execute() {
        for (int i = 0; i < number; i++) {
            commandNode.execute();
        }
    }
}
